package com.ggg.clothesshop.service.IMPL;

import com.ggg.clothesshop.mapper.UserMapper;
import com.ggg.clothesshop.model.User;
import com.ggg.clothesshop.service.UserService;
import com.ggg.clothesshop.utils.Md5;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceIMPL implements UserService {

    @Autowired
    private UserMapper userMapper;//这里会报错，但是并不会影响

    //根据id查找用户
    @Override
    public User selectUserById(int id) {
        User user = userMapper.selectByPrimaryKey(id);

        return user;
    }

    //查询一页用户信息
    @Override
    public PageInfo<User> selectByPage(int pageNum, int pageSize) {
        //将参数传给这个方法就可以实现物理分页了，非常简单。
        PageHelper.startPage(pageNum, pageSize);
        List<User> users = userMapper.selectUsers();
        PageInfo result = new PageInfo(users);
        return result;
    }

    //添加用户信息
    @Override
    public int addUser(User user) {
        //MD5 密码加密
        String password = Md5.md5Password(user.getPassword());
        //判断密码加密是否成功
        if("".equals(password)) {
            return 0;
        }
        user.setPassword(password);
        return userMapper.insertSelective(user);
    }

    //根据账号密码查找用户
    @Override
    public User selectUserByPassword(User user) {
        //MD5 密码加密
        String password = Md5.md5Password(user.getPassword());
        //判断密码加密是否成功
        if(!"".equals(password)) {
            user.setPassword(password);
        }
        return userMapper.selectByPassword(user);
    }

    //根据账号查询用户
    @Override
    public User selectByAccount(String account) {
        return userMapper.selectByAccount(account);
    }


}
